/*
 * Copyright (c) 2007, Romain Guy
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of the TimingFramework project nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package de.rockon.fuzzy.controller.util.effects;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;
import javax.swing.JFrame;

import de.rockon.fuzzy.exceptions.CouldNotBlurFrameException;

public class ProgressGlassPane extends JComponent {
	private static final long serialVersionUID = -2043430382609924042L;
	private static ProgressGlassPane singleton = null;
	private static boolean blurEnabled = true;
	
	
	public static ProgressGlassPane getInstance() {
		if (ProgressGlassPane.singleton == null) {
			ProgressGlassPane.singleton = new ProgressGlassPane();
		}
		return ProgressGlassPane.singleton;
	}

	private BufferedImage backDrop = null;

	private JFrame component;

	private ProgressGlassPane() {
		setBackground(Color.WHITE);
		setOpaque(true);
	}

	public void blur(JFrame component) throws CouldNotBlurFrameException {
		if (!blurEnabled) return;
		if (component == null) {
			throw new CouldNotBlurFrameException();
		}

		this.component = component;

		this.component.setGlassPane(this);
		this.component.getGlassPane().setVisible(true);

	}

	@Override
	protected void paintComponent(Graphics g) {
		// enables anti-aliasing
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

		int width = component.getRootPane().getWidth();
		int height = component.getRootPane().getHeight();
		g2.drawImage(backDrop, 0, 0, width, height, null);
	}

	@Override
	public void setVisible(boolean visible) {
		if (visible && backDrop == null) {
			backDrop = GraphicsUtilities.createCompatibleImage(component.getRootPane().getWidth(), component.getRootPane().getHeight());
			Graphics2D g2 = backDrop.createGraphics();
			component.getRootPane().paint(g2);
			g2.dispose();

			backDrop = GraphicsUtilities.createThumbnail(backDrop, component.getRootPane().getWidth() / 2);
			backDrop = new ColorTintFilter(Color.WHITE, 0.10f).filter(backDrop, null);
			backDrop = new GaussianBlurFilter(2).filter(backDrop, null);
		} else {
			if (backDrop != null) {
				backDrop.flush();
			}
			backDrop = null;
		}
		super.setVisible(visible);
	}

	/**
	 * unblurs the current window
	 * @throws CouldNotBlurFrameException
	 */
	public void unblur() throws CouldNotBlurFrameException {
		if (!blurEnabled) return;
		if (component == null) {
			throw new CouldNotBlurFrameException();
		}
		
		component.getGlassPane().setVisible(false);
		component = null;
	}

	
	/**
	 * @return zeigt an, ob blur aktiviert ist, oder nicht
	 */
	public static boolean isBlurEnabled() {
		return blurEnabled;
	}

	/**
	 * @param blurEnabled blur an oder aus
	 */
	public static void setBlurEnabled(boolean blurEnabled) {
		ProgressGlassPane.blurEnabled = blurEnabled;
	}
}
